package com.yan.test.work.testpaging3

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.cachedIn
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow

/**
 * Create By : 'YWY'
 * On : 2024/4/5
 */
class KTLViewModel :ViewModel() {
//    var data = Pager(
//        config = PagingConfig(
//            pageSize = 20,
//            initialLoadSize = 20,
//            enablePlaceholders = false,
//            prefetchDistance = 1
//        ),
//        pagingSourceFactory = {
//            MainPagingDataSource(AppGlobals.getApplication() , "Paging3")
//        }
//    ).flow.cachedIn(viewModelScope)


    suspend fun getDataList(keyWord:String):Flow<PagingData<DataBean>> {
        return  Pager(
            config = PagingConfig(
                pageSize = 20,
                initialLoadSize = 20,
                enablePlaceholders = false,
                prefetchDistance = 1
            ),
            pagingSourceFactory = {
                MainPagingDataSource(AppGlobals.getApplication() , keyWord)
            }
        ).flow.apply {
            cachedIn(viewModelScope)
        }
    }

}
